#include <DotSort/Level.hpp>

#include <cstdint>
#include <vector>

#include <DotSort/Tube.hpp>

Level::Level(uint8_t tubeSize, uint8_t tubeCount)
	: tubeSize(tubeSize)
	, tubeCount(tubeCount)
	, tubes(tubeCount, Tube(tubeSize))
{
}

bool Level::isPossibleStep(uint8_t t1, uint8_t t2) const
{
	if(this->tubes[t1].isCompleted())
	{
		return false;
	}
	if(this->tubes[t1].isEmpty())
	{
		return false;
	}
	if(this->tubes[t2].isFull())
	{
		return false;
	}
	if(this->tubes[t1].top() != this->tubes[t2].top())
	{
		return false;
	}
	if(this->tubes[t1].top() != this->tubes[t2].top())
	{
		return false;
	}
	return true;
}

void Level::doStep(uint8_t t1, uint8_t t2)
{
	this->tubes[t2].push(this->tubes[t1].top());
	this->tubes[t1].pop();
}

bool Level::isWin()
{
	for(Tube& tube : this->tubes)
	{
		if(!(tube.isCompleted() || tube.isEmpty()))
		{
			return false;
		}
	}
	return true;
}

const Tube& Level::operator[](uint8_t index) const
{
	return this->tubes[index];
}
